﻿app.controller('CIController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.GetDepartmentList = function (id) {
        $scope.DepartmentList = [];
        $http({
            method: 'POST',
            url: '../Organization/GetDepartmentList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.DepartmentList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }


    $scope.AddDepItems = function (list) {
        var model = {};
        var now = new Date();
        model.Score = 0;
        model.Years = now.getFullYear();
        list.push(model);
    }

    $scope.RemoveApplyItems = function (item, list) {
        let index = list.indexOf(item);
        list.splice(index, 1);
    }
    $scope.refreshOption = function (control) {
        $("#" + control).selectpicker('refresh');
    }
    $scope.GetAllUserList = function (control, defaultvalue) {
        
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../Construction/GetAllUserList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (defaultvalue.includes(n.Id + ""))
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });

    }
    $scope.AddCIDepForm = function (model, items) {
        model.TotalAmount = $scope.TotalAmount;
        var files = document.getElementById("taskfile").files;
        $scope.LoadingMsg = "正在提交,请稍后...";
        $scope.Submiting = true;
        Upload.upload({
            method: 'POST',
            url: '../CI/AddCIDepForm',
            data: { 'model': model, 'ItemList': items },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "#";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.AddCIEmpForm = function (model, items) {
        model.TotalAmount = $scope.TotalAmount;
        var files = document.getElementById("taskfile").files;
        $scope.LoadingMsg = "正在提交,请稍后...";
        $scope.Submiting = true;
        Upload.upload({
            method: 'POST',
            url: '../CI/AddCIEmpForm',
            data: { 'model': model, 'ItemList': items },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "#";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.GetCIDepForm = function (model, page, isload) {
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../CI/GetCIDepForm',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.ShowDepList = function (list) {
        $scope.DepItemList = list;
    }
    $scope.ShowEmpList = function (list) {
        $scope.EmpItemList = list;
    }

    $scope.DeleteNoiceForm = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该数据？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../CI/DeleteCIForm',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetCIDepForm(model, page,true);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.AddCINoticeForm = function (model) {
        var files = document.getElementById("taskfile").files;
        var editorInstance = CKEDITOR.instances['noticecontent'];
        model.NoticeContent = editorInstance.getData();
        $scope.LoadingMsg = "正在提交,请稍后...";
        $scope.Submiting = true;
        Upload.upload({
            method: 'POST',
            url: '../CI/AddCINoticeForm',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "#";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.GetCINoticeFormList = function (model, page, isload) {
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../CI/GetCINoticeFormList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteCINoticeForm = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该数据？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../CI/DeleteCINoticeForm',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetCINoticeFormList(model, page, true);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.GetCINoticeForm = function (Id) {
        $http({
            method: 'POST',
            url: '../CI/GetCINoticeForm',
            data: { 'Id': Id },
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.DataModel = ret.Data;
                    $("#noticecontent").html($scope.DataModel.NoticeContent);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }
})
